package com.example.shw

import android.media.MediaPlayer
import android.media.VolumeShaper
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.Gravity
import android.widget.Button
import android.widget.LinearLayout

class VolumeShaperActivity : AppCompatActivity(){
    private var mPlayer: MediaPlayer? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        var linearLayout = LinearLayout(this)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.gravity = Gravity.CENTER_VERTICAL
        val bn = Button(this)
        bn.text = "正常播放"
        val shaper = Button(this)
        shaper.text = "VolumeShaper播放"
        linearLayout.addView(bn)
        linearLayout.addView(shaper)
        setContentView(linearLayout)
        mPlayer = MediaPlayer.create(this, R.raw.imhappy)
        bn.setOnClickListener {
            mPlayer?.start()
        }
        shaper.setOnClickListener {
            val config = VolumeShaper.Configuration.Builder()
                    //设置插值方式
                .setInterpolatorType(VolumeShaper.Configuration.INTERPOLATOR_TYPE_LINEAR)
                .setCurve(floatArrayOf(0f, 0.5f, 1f),//时间点
                    floatArrayOf(0f, 1f, 0f))//各时间点对应的音量
                    //设置持续时间
                .setDuration(1000 * 60 * 2)
                .build()
            val volumeShaper = mPlayer?.createVolumeShaper(config)
            volumeShaper?.apply(VolumeShaper.Operation.PLAY)
        }
    }
}